<?php 
/**
 * PHP versions 4 and 5
 * MagicPHP
 * Licensed under The Open Group Test Suite License
 * Redistributions of files must retain the above copyright notice.
 * @copyright     Copyright 2011
 * @link          
 * @package       Model
 * @since         MaginPHP 1.0
 * @license     
 */
?>
<?php
/**
 * @desc: This Function Is Used To Generate Form Element using php 
 * @author: aNKIT kHAMBHATA
 */
class Magic_FormElement_Model {
    /**
     *@desc : This is private static variable
     */
    private $form ; 
    private $formAttr ;
    private static $_instance ;
    /**
     * @desc : This is the public method to call remaining function of class
     */
    public  static function callMethod(){
        self::$_instance = new self ;
        return self::$_instance ; 
    }
    /**
     * @author : aNKIT kHAMBHATA
     * @param  
     * @return input element 
     */
    public function createForm($arr){
        $str .= "<form" ;
        foreach ($arr as $k => $v) {
            $str .= "$k ='".$v."' ";
        }
        $str .=$this->form ;
        $str.= "</form>";
        return $str ; 
    }
    /**
     * @author : aNKIT kHAMBHATA
     * @param  $text : this function is used to insert text
     */
    public function text($text){
        $this->form .= $text ;
        return $this ;
    }
    /**
     * @author : aNKIT kHAMBHATA
     * @param  $arr : contain attributes and value array of input html element 
     * @return input element 
     */
    public function input($arr){
        $str = "<input ";
        foreach ($arr as $k => $v) {
            if($v != 'radio' || $v != 'checkbox')
            $str .= "$k ='".$v."' ";
        }
        
        $str .= html_entity_decode($str.">");
        $this->form .= $str ;
        return $this ;
    }
    /**
     * @author : aNKIT kHAMBHATA
     * @param  $arr : contain attributes and value array of checkbox
     * @return input element checkbox
     */
    public function checkbox($arr){
        if(isset($arr["title"]) && !empty($arr["title"])){
            $title = $arr["title"];
        } else {
            $title = '';
        }
        $str = $title ;     
        $str .= "<input='checkbox'";
        foreach ($arr as $k => $v) {
            if($k != 'type')
            $str .= "$k ='".$v."' ";
        }
        $str .= html_entity_decode($str.">");
        $this->form .= $str ; 
        return $this ;
    }
    /**
     * @author : aNKIT kHAMBHATA
     * @param  $arr : contain attributes and value array of checkbox group
     * @param $type : specify horizontal or vertical 
     * @return html entity checkbox groupbox
     */
    public function checkboxGroup($arr,$type ="horizontal"){
        $group=array();
        foreach ($arr["value"] as $k=>$v) {
            $valueArray = array("name"=>$arr["name"],
                            "id"=>$arr["id"],
                            "title"=>$k,
                            "value"=>$v
                        );
            
            $group[]= $this->checkbox($valueArray);
        }   
        if($type =="vertical"){
            
            return implode("</br>", $group);
        } elseif($type = 'horizontal') {
            return $group;
        }
    }
    /**
     * @author : aNKIT kHAMBHATA
     * @param  $arr : contain attributes and value array of radiobutton
     * @return input element checkbox
     */
    public function radiobutton($arr){
        if(isset($arr["title"]) && !empty($arr["title"])){
            $title = $arr["title"];
        } else {
            $title = '';
        }
        $str = $title ;
        $str .= "<input type='radio'";
        foreach ($arr as $k => $v) {
           
            if($k != 'type')
            $str .= "$k ='".$v."' ";
        }
        return html_entity_decode($str.">");
    }
     /**
     * @author : aNKIT kHAMBHATA
     * @param  $arr : contain attributes and value array of checkbox group
     * @param $type : specify horizontal or vertical 
     * @return html entity radio button group
     */
    public function radiobuttonGroup($arr ,$type = "horizontal"){
       
       $group=array();
        foreach ($arr["value"] as $k=>$v) {
            $valueArray = array("name"=>$arr["name"],
                            "id"=>$arr["id"],
                            "title"=>$k,
                            "value"=>$v
                        );
            //echo $this->radiobutton($valueArray);
            $group[]= $this->radiobutton($valueArray);
        }   
        if($type =="vertical"){
             return implode("</br>", $group);
        } else if($type == "horizontal") {
            return $group;
        }
    }
    /**
     * @desc : This is the function used to generate textarea
     * @param  $arr : contain attributes and value array of textarea html element 
     * @return textarea form element 
     */
    public function textarea($text){
        $te = "<textarea ";
        foreach ($text as $k=>$v){
            if($k != "value")
            $te .= "$k ='".$v."' ";
        }
        if(isset($text["value"])) {
            $te .=">".$text["value"];
        } else {
            $te .= ">";
        }
        return $te."</textarea>";
    }
    
}
/*
 * : input
 */
$checkboxGroup = array("name"=>'chk_group',
                        "id"=>'checkbox_ids',
                        "value"=>array("title1"=>"value1",
                                        "title2"=>"value2")
                    );
        
$t1 = array("type"=>"text",
                "id"=>"name",
                "name"=>"name",
                "value"=>"ankit",
                "calss"=>"required");
$t2 = array("type"=>"checkbox",
                "id"=>"name",
                "name"=>"name",
                "value"=>"ankit",
                "calss"=>"required");
$elem = array("type"=>"submit","class"=>"buttonbar");

$text = array("nane"=>"address",
                "id"=>"address",
                "class"=>"required",
                "col"=>"10",
                 "row"=>"10",
                 "value"=>"address");

$radio = array("name"=>"gender","value"=>male,"title"=>"male");
 //echo Magic_FormElement_Model::callMethod()->radiobuttonGroup($checkboxGroup,$type ="vertical");
//echo Magic_FormElement_Model::callMethod()->radiobutton($radio);
 
echo Magic_FormElement_Model::callMethod()
        ->text("<table border='1'><tr><td>Name</td><td>")->input($t1)->text("</td></tr>")
        ->text("<tr><td>City</td><td>")->input($t2)->text("</td></tr></table>")
        ->text("<tr><td></td><td><button type='submit' class='buttonbar' >submit</button></td></tr></table>")->createForm();
/**
   *type : text  
   */
    
                
          
         ?>  